package com.surfilter.msmp.common.filter;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 描述：页面向量加载器
 * 作者：JinHuaTao
 * 时间：2018/6/25 11:17
 */
public class PageVectorLoader {

    private static final Logger logger = LoggerFactory.getLogger(PageVectorLoader.class);

    private static final String SPLIT_NUMBER_SING = "#";

    private static final String SPLIT_COLON = ":";

    private static final String HYPHEN = "-";

    private static String config_path = "/fileRoot/siteFind/keyword/ssr.txt";

    private static List<PageVector> pageVectorList = null;


    public static void loader(){
        loader(null);
    }

    /**
     * 初始化词向量加载器
     * */
    public static void loader(String filePath){
        if(pageVectorList != null){
            return;
        }
        if(StringUtils.isNotBlank(filePath)){
            config_path = filePath;
        }
        pageVectorList = new ArrayList<>();
        try {
            List<String> lines = FileUtils.readLines(new File(config_path), "utf-8");
            for(String line : lines){
                String[] segs = line.split(SPLIT_NUMBER_SING);
                if(segs.length != 4){
                    continue;
                }
                String[] suffix = segs[0].split(SPLIT_COLON);
                String[] plusWord = segs[1].split(SPLIT_COLON);
                String[] middleWord = segs[2].split(SPLIT_COLON);
                String[] minusWord = segs[3].split(SPLIT_COLON);
                int number = Integer.parseInt(suffix[0]);
                PageVector pageVector = new PageVector(number, suffix[1], plusWord, middleWord, minusWord);
                pageVectorList.add(pageVector);
            }
        } catch (IOException e) {
            logger.info("Page vector configuration load error:{}", e);
        }
    }


    /**
     * 根据获取的HTML页面返回匹配的页面模板向量
     * */
    public static PageVector getPageVectorByHtml(final String html){
        if(pageVectorList == null){
            loader();
        }
        for(PageVector pageVector: pageVectorList){
            boolean plus = true;
            boolean middle = true;
            boolean minus = true;
            if(filterCondition(pageVector.getPlusWord())){
                plus = HpcFilter.everyWordMustMatch(html, pageVector.getPlusWord());
            }

            if(filterCondition(pageVector.getMiddleWord())){
                middle = HpcFilter.oneWordShouldMatch(html, pageVector.getMiddleWord());
            }

            if(filterCondition(pageVector.getMinusWord())){
                minus = HpcFilter.notOneWordMatch(html, pageVector.getMinusWord());
            }

            if(plus && middle && minus){
                return pageVector;
            }
        }
        return null;
    }

    private static boolean filterCondition(String[] strings){
        boolean flag = true;
        if(strings == null){
            flag = false;
        }else {
            if(strings[0].equals(HYPHEN)){
                flag = false;
            }
        }
        return flag;
    }


    /**
     * 根据编号返回匹配的页面模板向量
     * */
    public static PageVector getPageVectorByScore(final String scoreStr){
        if(pageVectorList == null){
            loader();
        }
        int score = Integer.parseInt(scoreStr);
        for(PageVector pageVector: pageVectorList){
            if(pageVector.getNumber() == score){
                return pageVector;
            }
        }
        return null;
    }

}
